package com.rsn.genxdgrid.jms;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created with IntelliJ IDEA.
 * User: Ravdeep
 * Date: 21/2/14
 * Time: 11:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class Producer {
    // URL of the JMS server. DEFAULT_BROKER_URL will just mean
    // that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the queue we will be sending messages to
    private static String subject = "TestQ";
    private static int ID =1;
    public static void main(String[] args) throws JMSException, InterruptedException {
        System.out.println("********  Started Inserting To TestQ *******");
        while(true){
            createMessage();
            Thread.sleep(10000);
        }

    }

    private static void createMessage() throws JMSException {

        ConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();


        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);


        Destination destination = session.createQueue(subject);


        MessageProducer producer = session.createProducer(destination);



        TextMessage message = session.createTextMessage("Test Message ID "+ID++);

        // Here we are sending the message!
        producer.send(message);
        System.out.println("Sent message '" + message.getText() + "'");

        connection.close();
    }
}